
from keras.models import load_model
from keras.preprocessing import image
import numpy as np
import shutil
OPERATION_DIR="./yjjy_tempt"
import os

def del_file(filepath):
    """
    删除某一目录下的所有文件或文件夹
    :param filepath: 路径
    :return:
    """
    del_list = os.listdir(filepath)
    for f in del_list:
        file_path = os.path.join(filepath, f)
        if os.path.isfile(file_path):
            os.remove(file_path)
        elif os.path.isdir(file_path):
            shutil.rmtree(file_path)

def predict_one_image(model,image_path):
    test_image = image.load_img(image_path, target_size = (250, 250)) 
    test_image = image.img_to_array(test_image)
    test_image = np.expand_dims(test_image, axis = 0)

    #predict the result
    result = model.predict(test_image)
    a=result.reshape((2,)).tolist()
    if a[0]>a[1]:
        return 0
    else:
        return 1

def get_bound_check_model():

    #os.environ["CUDA_VISIBLE_DEVICES"] = "0"
    

    print("loading bound_check model >>>")

    model=load_model(r"./model_data/bound_check.h5")

    print("loaded bound_check model>>>")
    return model


def predict_one_request(model,region_num,time):
    #print("loading >>>")
    #model = load_model(r"./model_data/bound_check.h5")


    predict_list=[]
    for i in range(region_num):
        predict_list.append(predict_one_image(model,os.path.join(os.path.join(OPERATION_DIR,time),str(i)+".jpg")))
    #delete predicted images
    #del_file(OPERATION_DIR)
    return predict_list

import time
from keras import  backend as K
if __name__ == '__main__':


    model = load_model(r"./model_data/bound_check.h5")
    for i in range(100):
        time.sleep(3)
        print(i)

    #print(predict_one_image(model,"2.png"))

    #
    # #predict_res=predict_one_image(model,"yjjy_tempt/0.png")-
    # #print(predict_res)
    # a=predict_one_request(3)
    # print(a)
    # print(predict_one_image(model,"./yjjy_tempt/1.png"))
    # print(predict_one_image(model,"3.jpg"))
    # print(predict_one_image(model,"3.png"))
    #del_file(OPERATION_DIR)